//
//  SelectionViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/4/7.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "SelectionViewController.h"
#import <BKKit/Theme.h>

@interface SelectionViewController ()
@end

@implementation SelectionViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
        self.hidesBottomBarWhenPushed = YES;
        self.maxSelectionCount = 1;
        self.selectedIndex = -1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(cancel:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(done:)];
    self.tableView.tableFooterView = [UIView new];
}
#pragma mark - action
- (void)cancel:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectionViewControllerDidCanceled:)]) {
        [self.delegate selectionViewControllerDidCanceled:self];
    }
}
- (void)done:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectionViewController:didSelectItem:)]) {
        SelectionItem *selectedItem = [self.items objectAtIndex:self.selectedIndex];
        [self.delegate selectionViewController:self didSelectItem:selectedItem];
    }
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.font = THEME_TEXT_FONT;
    }
    SelectionItem *item = [self.items objectAtIndex:indexPath.row];
    cell.textLabel.text = item.title;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedIndex >= 0 && self.maxSelectionCount == 1) {
        UITableViewCell *previousSelectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0]];
        if (previousSelectedCell) {
            previousSelectedCell.accessoryView = nil;
        }
    }
    self.selectedIndex = indexPath.row;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    SelectionItem *selectedItem = [self.items objectAtIndex:indexPath.row];
    [self.delegate selectionViewController:self didSelectItem:selectedItem];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation SelectionItem
- (instancetype)initWithTitle:(NSString *)title value:(id)value{
    if (self = [super init]) {
        self.title = title;
        self.value = value;
    }
    return self;
}
@end
